home *** CD-ROM | disk | FTP | other *** search
/ By Popular Request 2.0 / By Popular Request 2.0 (Arsenal Computer).ISO / amiga_6 / tiffdtyp.lha / sources / flzw.asm < prev    next >
Assembly Source File  |  1995-05-21  |  4KB  |  161 lines

  1.     opt l+
  2.  
  3.             XDEF    _UnPackLZW
  4.         
  5.             XREF    _StringTable
  6.  
  7. EOICODE     EQU    257
  8. CLEARCODE     EQU 256
  9.  
  10. LEN         EQU    4
  11. LASTCHAR    EQU 6
  12. FIRSTCHAR    EQU 7
  13.  
  14. ;    The StringTable entry structure is defined as:
  15. ;
  16. ;    struct StringEntry 
  17. ;        {
  18. ;            struct StringEntry *OldCode;
  19. ;            UWORD  StringLen;
  20. ;            UBYTE  LastChar;
  21. ;            UBYTE  FirstChar;
  22. ;        }
  23.  
  24.  
  25. ;    Adds a string in the string table
  26. ;
  27. ;    a1 = &StringTable[CurrentStringEntry]
  28. ;   a2 = &StringTable[OldCode]
  29. ;    d0 = Code
  30. ;    d1 = TabEntries*sizeof(struct StringEntry)
  31.  
  32. _AddStringToTable
  33.         
  34.         move.l    _StringTable,a0
  35.         asl.l    #3,d0        ; d0 = d0 * sizeof(struct StringEntry)
  36.         add.l    d0,a0        ; a0 = &StringTable[Code]
  37.         cmp.w    d1,d0        ; if(Code < TabEntries) 
  38.         bge.s    .NotInTable ;    {
  39.         move.l    LEN(a0),d2    ;    d2 = StringTable[Code].FirstChar; 
  40.         bra.s    .Endif        ;    }
  41. .NotInTable                    ; else
  42.         move.l    LEN(a2),d2    ;    d2 = StringTable[OldCode].FirstChar; 
  43. .Endif                                    
  44.         move.l    a2,(a1)+    ; StringTable[CurrentStringEntry].OldCode = &StringTable[OldCode]
  45.         move.l    LEN(a2),(a1)+    ; StringTable[CurrentStringEntry].StringLen = StringTable[OldCode].StringLen
  46.                                 ; StringTable[CurrentStringEntry].FirstChar = StringTable[OldCode].FirstChar
  47.                                 ; StringTable[CurrentStringEntry].LastChar  = StringTable[OldCode].LastChar
  48.         addq.w    #1,-4(a1)    ; StringTable[CurrentStringEntry].StringLen ++;
  49.         move.b    d2,-2(a1)    ; StringTable[CurrentStringEntry].LastChar = StringTable[Code].FirstChar
  50.         move.l    a0,a2        ; OldCode = Code
  51.         addq.w    #8,d1        ; TabEntries++
  52.         cmp.w    #511*8,d1
  53.         beq.s    .IncCodeLen
  54.         cmp.w    #1023*8,d1
  55.         beq.s    .IncCodeLen
  56.         cmp.w    #2047*8,d1
  57.         beq.s    .IncCodeLen
  58.         rts
  59.  
  60. .IncCodeLen
  61.         addq.w    #1,d4
  62.         rts
  63.  
  64. ;    Write a string to the output buffer
  65. ;
  66. ;    a3 = OutputBuffer
  67. ;    a0 = &StringTable[Code]
  68.  
  69. _WriteString
  70.         clr.l    d2
  71.         move.w    LEN(a0),d2    ; d2 = StringTable[Code].Len
  72.         add.l    d2,a3        ; a3 = OutBuf+Len
  73.         move.l    a3,a5        ; a5 = OutBuf+Len
  74.         sub.w    #1,d2
  75. .loop                        ; for(i=0;i<Len;i++)
  76.         move.b    LASTCHAR(a0),-(a5)    ; OutBuf[i]= StringTable[Code].LastChar
  77.         move.l    (a0),a0        ; a0 = StringTable[Code].OldCode
  78.         dbf        d2,.loop
  79.         rts
  80.  
  81. ;    Get the next code from the input buffer
  82. ;
  83. ;    d4 = CodeLen
  84. ;    d5 = lastbit
  85. ;    d6 = InCode
  86. ;    a4 = InBuffer
  87.  
  88. _GetNextCode
  89.         
  90. .while
  91.     cmp.b    d4,d5        ;    while( lastbit < CodeLen )
  92.     bge        .BufferFull    ;    {
  93.     move.b    (a4)+,d6    ;        InCode.b[3]=*InBuff++;
  94.     lsl.l    #8,d6        ;        InCode.l << = 8;
  95.     addq.b    #8,d5        ;        lastbit +=8;
  96.     bra.s    .while        ;    }
  97. .BufferFull
  98.     move.b    (a4)+,d6    ;    InCode.b[3]=*InBuff++;
  99. ;    lsl.l    #8,d6        ;    InCode.l << = 8;
  100.     sub.b    d4,d5        ;   lastbit=lastbit - CodeLen
  101.     moveq.l    #8,d0
  102.     sub.b    d5,d0        ;     d0 = 8 -(lastbit-CodeLen)
  103.     lsl.l    d0,d6        ;    Incode << = 8-(lastbit-CodeLen)
  104.     move.l    d6,d0        
  105.     clr.w    d0
  106.     swap    d0            ;    Code = InCode.w[0]
  107.     and.l    #$ffff,d6
  108.     lsl.l    d5,d6        ;    Incode << = lastbit
  109.     addq.b    #8,d5        ;   lastbit +=8
  110.     rts
  111.  
  112. _InitStringTable
  113.     move.l    #258*8,d1    ; TabEntries = 258
  114.     moveq.l #9,d4        ; CodeLen = 9
  115.     move.l    _StringTable,a1
  116.     add.l    d1,a1        ; a1 = &StringTable[Currentry]
  117.     rts
  118.  
  119. ;
  120. ;    Unpack a block of LZW data
  121. ;
  122. ;    a0 = InBuffer
  123. ;    a1 = OutBuffer
  124.  
  125. _UnPackLZW
  126.     
  127.     movem.l    a2-a5/d2-d7,-(a7)
  128.     move.l    a0,a4        ; a4 = InBuffer
  129.     move.l    a1,a3        ; a3 = OutBuffer
  130.     move.l    a1,outbuf    ; outbuf = OutBuffer
  131.  
  132.     clr.l    d5
  133.     clr.l    d6
  134.     jsr        _InitStringTable        
  135. .next
  136.     jsr        _GetNextCode ; d0 = Code
  137.     cmp.w    #EOICODE,d0
  138.     beq        .exit
  139.     cmp.w    #CLEARCODE,d0
  140.     bne.s    .noclear
  141.     jsr        _InitStringTable        
  142.     jsr        _GetNextCode ; d0 = Code
  143.     cmp.w    #EOICODE,d0
  144.     beq.s    .exit
  145.     move.b    d0,(a3)+
  146.     asl.l    #3,d0
  147.     move.l    _StringTable,a2
  148.     add.l    d0,a2
  149.     bra.s    .next
  150. .noclear
  151.     jsr        _AddStringToTable
  152.     jsr        _WriteString
  153.     bra.s    .next
  154. .exit
  155.     move.l    a3,d0
  156.     sub.l    outbuf,d0
  157.     movem.l    (a7)+,a2-a5/d2-d7
  158.     rts
  159.  
  160. outbuf ds.l        1
  161.